package com.ai.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.util.Collections;

/**
 * CodeGenerator
 * Description:
 * Author: fenix
 * Date: 2023/3/26/026 22:01
 */

public class CodeGenerator {

    private static final String IP = "127.0.0.1";
    private static final String PORT = "3306";
    private static final String DATABASE_NAME = "ningsteel";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    private static final String OUTPUT_PATH = System.getProperty("user.dir") + "/src/main/java";
    private static final String XML_OUTPUT_PATH = System.getProperty("user.dir") + "/src/main/resources/xml";

    public static void main(String[] args) {
        codeGenerate("sys_user", "sys_role", "sys_permission", "sys_user_role_relation", "sys_role_permission_relation", "sys_request_path", "sys_request_path_permission_relation");
    }

    private static void codeGenerate(String... tableName) {
        FastAutoGenerator.create("jdbc:mysql://" + IP + ":" + PORT + "/" + DATABASE_NAME + "?characterEncoding=utf8", USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("chen") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .dateType(DateType.ONLY_DATE)   //修改Date类型
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .outputDir(OUTPUT_PATH); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.ai.security") // 设置父包名
//                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, XML_OUTPUT_PATH)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix("t_"); // 设置过滤表前缀

                    //Entity 策略配置
                    builder.entityBuilder()
                            .enableLombok() //开启 lombok 模型
                            .enableTableFieldAnnotation()   //开启生成实体时生成字段注解
                            /* 逻辑删除@TableLogic注解属性:
                             *  value = "未删除的值，默认值为0";若设置为2，则查询时 where 后面自动拼接 is_del = 2
                             *  delval = "删除后的值，默认值为1"
                             * 生成后需手动给该字段赋默认值或数据库指定默认值，不然insert默认为null会查不到(xxx where deleted = 0) */
                            .logicDeleteColumnName("is_del")   //逻辑删除字段名(数据库)
                            .logicDeletePropertyName("deleteFlag")  //逻辑删除属性名(实体)
                            .addTableFills(new Property("createTime", FieldFill.INSERT))    // 添加表字段填充
                            .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                            .idType(IdType.AUTO);    //全局主键类型

                    //Controller 策略配置
                    builder.controllerBuilder()
                            .enableRestStyle()  //开启生成@RestController 控制器
                            .enableHyphenStyle();

                    builder.serviceBuilder();
//                            .formatServiceFileName("I%sService");//格式化 service 接口文件名称(它默认生成的会前面加个I -> IUserService)

                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
